package com.ymatou.iapi.event.auth.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.event.auth.parameter.TokenHandleBean;
import com.ymatou.iapi.event.auth.service.TokenHandleCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;
/**
*token日志
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_TokenHandle{
	private static TokenHandleBean tokenhandleBean;
	private static TokenHandleCall tokenhandleCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("token日志");
	}

	@Before
	public void caseUp() {
		tokenhandleBean=new TokenHandleBean();
		tokenhandleCall=new TokenHandleCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(tokenhandleCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
//	seller.app.ymatou.com
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TokenHandle_001() {
		Logger.start(true,"记录日志成功");
		try {
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setRegisterType(1);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid=uc.getUserid();
			Thread.sleep(1000);
			tokenhandleBean.setUserId(userid);
			tokenhandleBean.setAppType("seller.app.ymatou.com");
			tokenhandleBean.setChannelSource("IOS");
			tokenhandleBean.setRepCode(200);
			tokenhandleBean.setRepMsg("OK");
			tokenhandleBean.setRunTime("1.00");
			tokenhandleBean.setTerminalSource("AutoTest");
			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
			tokenhandleBean.setClientIp(TestDataManager.getRandomIp());
			tokenhandleCall.setData(tokenhandleBean);
			tokenhandleCall.callService();			
			Logger.verifyEquals("ok", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
//			List<Map> lm=new LoginHistoryWapper().selectLastTokenHistoryByTokenId(tokenhandleBean.getToken());
//			Logger.verifyEquals(1, lm.size(), "验证记录条数");
//			Map m=lm.get(0);
//			Logger.verifyEquals(tokenhandleBean.getUserId(), m.get("UserId"), "验证tokenhandle的UserId");
//			Logger.verifyEquals(tokenhandleBean.getRepMsg(), m.get("RepMsg"), "验证tokenhandle的RepMsg");
//			Logger.verifyEquals(tokenhandleBean.getTerminalSource(), m.get("TerminalSource"), "验证tokenhandle的TerminalSource");
//			Logger.verifyEquals(tokenhandleBean.getRepCode(), m.get("RepCode"), "验证tokenhandle的RepCode");
//			Logger.verifyEquals(tokenhandleBean.getChannelSource(), m.get("ChannelSource"), "验证tokenhandle的ChannelSource");
			List<Map> online = new UserSafeWapper().selectSelleronlineStatus(userid, null);
			Logger.debug(online);
			Logger.verifyEquals("30002", online.get(0).get("OpType"), "验证tokenhandle的OpType");
			Logger.verifyEquals("TokenAuthJAVA", online.get(0).get("OpDescript"), "验证tokenhandle的OpDescript");
			Logger.verifyEquals(tokenhandleBean.getAppType(), online.get(0).get("AppSource"), "验证tokenhandle的AppSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

//	sloginapp.ymatou.com
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_TokenHandle_002() {
		Logger.start(true,"sloginapp.ymatou.com记录日志成功");
		try {
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setRegisterType(1);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid=uc.getUserid();
			Thread.sleep(1000);
			tokenhandleBean.setUserId(userid);
			tokenhandleBean.setAppType("sloginapp.ymatou.com");
			tokenhandleBean.setChannelSource("IOS");
			tokenhandleBean.setRepCode(200);
			tokenhandleBean.setRepMsg("OK");
			tokenhandleBean.setRunTime("1.00");
			tokenhandleBean.setTerminalSource("AutoTest");
			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
			tokenhandleCall.setData(tokenhandleBean);
			tokenhandleCall.callService();			
			Logger.verifyEquals("ok", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
			List<Map> online = new UserSafeWapper().selectSelleronlineStatus(userid, null);
			Logger.debug(online);
			Logger.verifyEquals("30002", online.get(0).get("OpType"), "验证tokenhandle的OpType");
			Logger.verifyEquals("TokenAuthJAVA", online.get(0).get("OpDescript"), "验证tokenhandle的OpDescript");
			Logger.verifyEquals(tokenhandleBean.getAppType(), online.get(0).get("AppSource"), "验证tokenhandle的AppSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	app_sellerauth.iapi.ymatou.com
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_TokenHandle_003() {
		Logger.start(true,"app_sellerauth.iapi.ymatou.com记录日志成功");
		try {
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setRegisterType(1);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid=uc.getUserid();
			Thread.sleep(1000);
			tokenhandleBean.setUserId(userid);
			tokenhandleBean.setAppType("app_sellerauth.iapi.ymatou.com");
			tokenhandleBean.setChannelSource("IOS");
			tokenhandleBean.setRepCode(200);
			tokenhandleBean.setRepMsg("OK");
			tokenhandleBean.setRunTime("1.00");
			tokenhandleBean.setTerminalSource("AutoTest");
			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
			tokenhandleCall.setData(tokenhandleBean);
			tokenhandleCall.callService();			
			Logger.verifyEquals("ok", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
			List<Map> online = new UserSafeWapper().selectSelleronlineStatus(userid, null);
			Logger.debug(online);
			Logger.verifyEquals("30002", online.get(0).get("OpType"), "验证tokenhandle的OpType");
			Logger.verifyEquals("TokenAuthJAVA", online.get(0).get("OpDescript"), "验证tokenhandle的OpDescript");
			Logger.verifyEquals(tokenhandleBean.getAppType(), online.get(0).get("AppSource"), "验证tokenhandle的AppSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

//	PC:
//	slogin.ymatou.com
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_TokenHandle_004() {
		Logger.start(true,"slogin.ymatou.com记录日志成功");
		try {
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setRegisterType(1);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid=uc.getUserid();
			Thread.sleep(1000);
			tokenhandleBean.setUserId(userid);
			tokenhandleBean.setAppType("slogin.ymatou.com");
			tokenhandleBean.setChannelSource("IOS");
			tokenhandleBean.setRepCode(200);
			tokenhandleBean.setRepMsg("OK");
			tokenhandleBean.setRunTime("1.00");
			tokenhandleBean.setTerminalSource("AutoTest");
			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
			tokenhandleCall.setData(tokenhandleBean);
			tokenhandleCall.callService();			
			Logger.verifyEquals("ok", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
			List<Map> online = new UserSafeWapper().selectSelleronlineStatus(userid, null);
			Logger.debug(online);
			Logger.verifyEquals("30001", online.get(0).get("OpType"), "验证tokenhandle的OpType");
			Logger.verifyEquals("TokenAuthJAVA", online.get(0).get("OpDescript"), "验证tokenhandle的OpDescript");
			Logger.verifyEquals(tokenhandleBean.getAppType(), online.get(0).get("AppSource"), "验证tokenhandle的AppSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	pc_sellerauth.iapi.ymatou.com
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_TokenHandle_005() {
		Logger.start(true,"pc_sellerauth.iapi.ymatou.com记录日志成功");
		try {
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setRegisterType(1);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid=uc.getUserid();
			Thread.sleep(1000);
			tokenhandleBean.setUserId(userid);
			tokenhandleBean.setAppType("pc_sellerauth.iapi.ymatou.com");
			tokenhandleBean.setChannelSource("IOS");
			tokenhandleBean.setRepCode(200);
			tokenhandleBean.setRepMsg("OK");
			tokenhandleBean.setRunTime("1.00");
			tokenhandleBean.setTerminalSource("AutoTest");
			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
			tokenhandleCall.setData(tokenhandleBean);
			tokenhandleCall.callService();			
			Logger.verifyEquals("ok", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
			List<Map> online = new UserSafeWapper().selectSelleronlineStatus(userid, null);
			Logger.debug(online);
			Logger.verifyEquals("30001", online.get(0).get("OpType"), "验证tokenhandle的OpType");
			Logger.verifyEquals("TokenAuthJAVA", online.get(0).get("OpDescript"), "验证tokenhandle的OpDescript");
			Logger.verifyEquals(tokenhandleBean.getAppType(), online.get(0).get("AppSource"), "验证tokenhandle的AppSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TokenHandle_006() {
		Logger.start(true,"商家主子账号token认证,查看服务器日志saveSellerOnLineOp_JIRACONN-14043");
		//http://selleraccount.iapi.ymatou.com/api/sellerqueryapi/sellersubaccount/getalllist?onlyidlist=true 查询主子账号
		try {
			Integer userid=20339766;
			tokenhandleBean.setUserId(userid);
			tokenhandleBean.setAppType("seller.app.ymatou.com");
			tokenhandleBean.setChannelSource("IOS");
			tokenhandleBean.setRepCode(200);
			tokenhandleBean.setRepMsg("OK");
			tokenhandleBean.setRunTime("1.00");
			tokenhandleBean.setTerminalSource("AutoTest");
			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
			tokenhandleCall.setData(tokenhandleBean);
			tokenhandleCall.callService();			
			Logger.verifyEquals("ok", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
//			List<Map> lm=new LoginHistoryWapper().selectLastTokenHistoryByTokenId(tokenhandleBean.getToken());
//			Logger.verifyEquals(1, lm.size(), "验证记录条数");
//			Map m=lm.get(0);
//			Logger.verifyEquals(tokenhandleBean.getUserId(), m.get("UserId"), "验证tokenhandle的UserId");
//			Logger.verifyEquals(tokenhandleBean.getRepMsg(), m.get("RepMsg"), "验证tokenhandle的RepMsg");
//			Logger.verifyEquals(tokenhandleBean.getTerminalSource(), m.get("TerminalSource"), "验证tokenhandle的TerminalSource");
//			Logger.verifyEquals(tokenhandleBean.getRepCode(), m.get("RepCode"), "验证tokenhandle的RepCode");
//			Logger.verifyEquals(tokenhandleBean.getChannelSource(), m.get("ChannelSource"), "验证tokenhandle的ChannelSource");
			List<Map> online = new UserSafeWapper().selectSelleronlineStatus(3383, null);
			Logger.debug(online);
			Logger.verifyEquals("30002", online.get(0).get("OpType"), "验证tokenhandle的OpType");
			Logger.verifyEquals("TokenAuthJAVA", online.get(0).get("OpDescript"), "验证tokenhandle的OpDescript");
			Logger.verifyEquals(tokenhandleBean.getAppType(), online.get(0).get("AppSource"), "验证tokenhandle的AppSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TokenHandle_007() {
		Logger.start(true,"ip过长记录日志成功JIRACONN-14324");
		try {
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setRegisterType(1);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid=uc.getUserid();
			Thread.sleep(1000);
			tokenhandleBean.setUserId(userid);
			tokenhandleBean.setAppType("seller.app.ymatou.com");
			tokenhandleBean.setChannelSource("IOS");
			tokenhandleBean.setRepCode(200);
			tokenhandleBean.setRepMsg("OK");
			tokenhandleBean.setRunTime("1.00");
			tokenhandleBean.setTerminalSource("AutoTest");
			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
			String ip="";
			for(int i=0;i<10;i++){
				ip+=TestDataManager.getRandomIp()+";";
			}
			tokenhandleBean.setClientIp(ip);
			tokenhandleCall.setData(tokenhandleBean);
			tokenhandleCall.callService();			
			Logger.verifyEquals("ok", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
//			List<Map> lm=new LoginHistoryWapper().selectLastTokenHistoryByTokenId(tokenhandleBean.getToken());
//			Logger.verifyEquals(1, lm.size(), "验证记录条数");
//			Map m=lm.get(0);
//			Logger.verifyEquals(tokenhandleBean.getUserId(), m.get("UserId"), "验证tokenhandle的UserId");
//			Logger.verifyEquals(tokenhandleBean.getRepMsg(), m.get("RepMsg"), "验证tokenhandle的RepMsg");
//			Logger.verifyEquals(tokenhandleBean.getTerminalSource(), m.get("TerminalSource"), "验证tokenhandle的TerminalSource");
//			Logger.verifyEquals(tokenhandleBean.getRepCode(), m.get("RepCode"), "验证tokenhandle的RepCode");
//			Logger.verifyEquals(tokenhandleBean.getChannelSource(), m.get("ChannelSource"), "验证tokenhandle的ChannelSource");
			List<Map> online = new UserSafeWapper().selectSelleronlineStatus(userid, null);
			Logger.debug(online);
			Logger.verifyEquals("30002", online.get(0).get("OpType"), "验证tokenhandle的OpType");
			Logger.verifyEquals("TokenAuthJAVA", online.get(0).get("OpDescript"), "验证tokenhandle的OpDescript");
			Logger.verifyEquals(tokenhandleBean.getAppType(), online.get(0).get("AppSource"), "验证tokenhandle的AppSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	@Test
//	@Category({P3.class})
//	@TestCase
//	public void Tc_TokenHandle_002() {
//		Logger.start(true,"不输入usreid，失败");
//		try {
////			tokenhandleBean.setUserId(1);
//			tokenhandleBean.setChannelSource("IOS");
//			tokenhandleBean.setRepCode(200);
//			tokenhandleBean.setRepMsg("OK");
//			tokenhandleBean.setRunTime("1.00");
//			tokenhandleBean.setTerminalSource("AutoTest");
//			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
//			tokenhandleCall.setData(tokenhandleBean);
//			tokenhandleCall.callService();			
//			Logger.verifyEquals("fail", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
//	@Test
//	@Category({P3.class})
//	@TestCase
//	public void Tc_TokenHandle_003() {
//		Logger.start(true,"不输入repMsg，失败");
//		try {
//			tokenhandleBean.setUserId(1);
//			tokenhandleBean.setChannelSource("IOS");
//			tokenhandleBean.setRepCode(200);
////			tokenhandleBean.setRepMsg("OK");
//			tokenhandleBean.setRunTime("1.00");
//			tokenhandleBean.setTerminalSource("AutoTest");
//			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
//			tokenhandleCall.setData(tokenhandleBean);
//			tokenhandleCall.callService();			
//			Logger.verifyEquals("fail", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
//	@Test
//	@Category({P3.class})
//	@TestCase
//	public void Tc_TokenHandle_004() {
//		Logger.start(true,"不输入Token，失败");
//		try {
//			tokenhandleBean.setUserId(1);
//			tokenhandleBean.setChannelSource("IOS");
//			tokenhandleBean.setRepCode(200);
//			tokenhandleBean.setRepMsg("OK");
//			tokenhandleBean.setRunTime("1.00");
//			tokenhandleBean.setTerminalSource("AutoTest");
////			tokenhandleBean.setToken(TestDataManager.getRandomUUID().toString());
//			tokenhandleCall.setData(tokenhandleBean);
//			tokenhandleCall.callService();			
//			Logger.verifyEquals("fail", tokenhandleCall.getReturnData(), "验证tokenhandle的Returndata");	
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
}